using System;
using System.Web.UI;
using Tarantino.Samples.Core.Model;
using StructureMap;
using Tarantino.Core.Commons.Services.Repositories;

namespace Tarantino.Samples.UI
{
	public partial class DeleteSystemUser : Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if (isExistingSystemUser())
			{
				SystemUser systemUser = getExistingSystemUser();

				IPersistentObjectRepository repository = ObjectFactory.GetInstance<IPersistentObjectRepository>();
				repository.Delete(systemUser);
			}
			
			Response.Redirect("ViewSystemUsers.aspx");
		}

		private SystemUser getExistingSystemUser()
		{
			IPersistentObjectRepository repository = ObjectFactory.GetInstance<IPersistentObjectRepository>();
			return repository.GetById<SystemUser>(getId());
		}

		private Guid getId()
		{
			return new Guid(Request.Params["id"]);
		}

		private bool isExistingSystemUser()
		{
			bool isExisting = Request.Params["id"] != null;
			return isExisting;
		}
	}
}